package com.module.base.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author yanxuwen
 * @date 2018/7/19 16:27
 * @desc
 */
public class TimeUtils {

    public static final String FULL_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String HOUR_FORMAT = "HH:mm";
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String DATE_MOUTH_FORMAT = "yyyy-MM";
    public static final String DATE_M_D_H_M = "MM月dd日 HH:mm";


    public static String getDate(Date date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    /**
     * 传yyyy-MM-dd HH:mm:ss 、 MM-dd HH:mm:ss这样样式格式，
     */
    public static String getDate(long time, String pattern) {
        time = getMillSecond(time);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(time);
    }

    public static String date2FullStr(Date date) {
        return getDate(date, FULL_FORMAT);
    }

    public static String time2FullStr(long timestamp) {
        return getDate(timestamp, FULL_FORMAT);
    }

    public static String time2HourStr(long timestamp) {
        return getDate(timestamp, HOUR_FORMAT);
    }


    public static String date2Str(Date date) {
        return getDate(date, DATE_FORMAT);
    }

    public static String date2Str(long timestamp) {
        return getDate(timestamp, DATE_FORMAT);
    }

    public static String date2MouthStr(Date date) {
        return getDate(date, DATE_MOUTH_FORMAT);
    }

    public static String date2MouthStr(long timestamp) {
        return getDate(timestamp, DATE_MOUTH_FORMAT);
    }

    public static String time2MDHM(long timestamp) {
        return getDate(timestamp, DATE_M_D_H_M);

    }

    /**
     * 获取毫秒
     */
    public static long getMillSecond(long time) {
        if (String.valueOf(time).length() <= 10) {
            time = time * 1000L;
        }
        return time;
    }

    /**
     * 获取秒
     */
    public static long getSecond(long time) {
        if (String.valueOf(time).length() > 10) {
            time = time / 1000L;
        }
        return time;
    }

    public static boolean isToday(long timestamp) {
        Calendar c = Calendar.getInstance();
        clearCalendar(c, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND);
        c.add(Calendar.DAY_OF_MONTH, 0);
        long firstOfDay = c.getTimeInMillis(); // 昨天最早时间

        c.setTimeInMillis(timestamp);
        clearCalendar(c, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND); // 指定时间戳当天最早时间

        return firstOfDay == c.getTimeInMillis();
    }

    private static void clearCalendar(Calendar c, int... fields) {
        for (int f : fields) {
            c.set(f, 0);
        }
    }

    /**
     * 判断是否同一天
     */
    public static boolean isSameDay(long time, long time2) {
        if (getDate(time, DATE_FORMAT).equals(getDate(time2, DATE_FORMAT))) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否同一天
     */
    public static boolean isSameMonth(long time, long time2) {
        if (getDate(time, DATE_MOUTH_FORMAT).equals(getDate(time2, DATE_MOUTH_FORMAT))) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否同一天
     */
    public static boolean isSameYear(long time, long time2) {
        if (getDate(time, "yyyy").equals(getDate(time2, "yyyy"))) {
            return true;
        }
        return false;
    }

    /**
     * 时间差，返回多少分
     */
    public static long getDiffMinute(long time, long time2) {
        long diff = Math.abs(getSecond(time) - getSecond(time2));  //获取时间差
        return diff / 60;
    }


    /**
     * 时间差，返回多少分
     */
    public static long getDiffMinute(int hours, int minutes, int hours_end, int minutes_end) {
        long time = setTime(getToday(), hours, minutes);
        long time2 = setTime(getToday(), hours_end, minutes_end);
        return getDiffMinute(time, time2);
    }

    /**
     * 获取今天，且为0点0分
     */
    public static long getToday() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * .
     * 获取明天，且为0点0分
     */
    public static long getTomorrow() {
        return getToday() + 1000 * 60 * 60 * 24;
    }

    /**
     * .
     * 获取昨天，且为0点0分
     */
    public static long getYesterday() {
        return getToday() - 1000 * 60 * 60 * 24;
    }

    /**
     * 填入时 和 分
     * 设置时间
     */
    public static long setTime(long time, int hours, int minutes) {
        time = getMillSecond(time);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        calendar.set(Calendar.HOUR_OF_DAY, hours);
        calendar.set(Calendar.MINUTE, minutes);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 舍弃秒，，如 2021-07-15 17:43:50
     * 就好变成 2021-07-15 17:43:00
     */
    public static long getTimeMinute(long time) {
        time = getMillSecond(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 倒计时
     */
    public static String countDown(long time) {
        time = time * 1000;
        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = time / dd;
        long hour = (time - day * dd) / hh;
        long minute = (time - day * dd - hour * hh) / mi;
        long second = (time - day * dd - hour * hh - minute * mi) / ss;

        String strMinute = minute < 10 ? "0" + minute : "" + minute;
        String strSecond = second < 10 ? "0" + second : "" + second;

        hour = day * 24 + hour;//倒计时不需要天
        String strHour = hour < 10 ? "0" + hour : "" + hour;

        if (hour != 0) {
            return strHour + ":" + strMinute + ":" + strSecond + "";
        } else {
            return strMinute + ":" + strSecond + "";
        }

    }

    /**单位毫秒*/
    public static String getVideoTime(long time) {
        if (time > 1000l * 60 * 60) {
            return getDate(time, "HH:mm:ss");
        } else {
            return getDate(time, "mm:ss");
        }
    }


    /**
     * 倒计时
     */
    public static String getHHMM(long time) {
        time = time * 1000;
        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = time / dd;
        long hour = (time - day * dd) / hh;
        long minute = (time - day * dd - hour * hh) / mi;
        long second = (time - day * dd - hour * hh - minute * mi) / ss;

        String strMinute = minute < 10 ? "0" + minute : "" + minute;
        String strSecond = second < 10 ? "0" + second : "" + second;

        hour = day * 24 + hour;//倒计时不需要天
        String strHour = hour < 10 ? "0" + hour : "" + hour;

        if (hour != 0) {
            return strHour + ":" + strMinute + ":" + strSecond + "";
        } else {
            return strMinute + ":" + strSecond + "";
        }

    }

}
